//
//  UIImage+category.m
//  ios-loan
//
//  Created by guoqingping on 2017/12/22.
//  Copyright © 2017年 hxhg. All rights reserved.
//

#import "UIImage+category.h"

@implementation UIImage (category)
+ (UIImage *)imageWithColor:(UIColor *)maskColor{
    return [self imageWithColor:maskColor size:CGSizeMake(1.0, 1.0)];
}

+(UIImage *)imageWithColor:(UIColor *)maskColor size:(CGSize)size{
    CGRect rect = CGRectMake(0.0f, 0.0f, size.width, size.height);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [maskColor CGColor]);
    CGContextFillRect(context, rect);
    UIImage *resultImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return resultImage;
}

- (void)roundImageWithSize:(CGSize)size fillColor:(UIColor *)fillColor opaque:(BOOL)opaque completion:(void (^)(UIImage *))completion {
    
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        // 1. 利用绘图，建立上下文 BOOL选项为是否为不透明
        UIGraphicsBeginImageContextWithOptions(size, opaque, 0);
        
        CGRect rect = CGRectMake(0, 0, size.width, size.height);
        
        // 2. 设置填充颜色
        if (opaque) {
            [fillColor setFill];
            UIRectFill(rect);
        }
        
        // 3. 利用 贝赛尔路径 `裁切 效果
        UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:rect];
        
        [path addClip];
        
        // 4. 绘制图像 如果图片为空那么为单色渲染
        if (self) {
            [self drawInRect:rect];
        }
        
        // 5. 取得结果
        UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
        
        // 6. 关闭上下文
        UIGraphicsEndImageContext();
        
        // 7. 完成回调
        dispatch_async(dispatch_get_main_queue(), ^{
            if (completion != nil) {
                completion(result);
            }
        });
    });
}

+(UIImage*)resizeImageWithCenterMethod:(NSString*)name{
    UIImage *image = [UIImage imageNamed:name];
    //顶端盖的高度
    CGFloat top = CGFLOAT_MIN;
    CGFloat bottom = CGFLOAT_MIN;//低端盖的高度
    CGFloat left = CGFLOAT_MIN;//左端盖的高度
    CGFloat right = CGFLOAT_MIN;//右端盖的高度
    UIEdgeInsets insets = UIEdgeInsetsMake(top, left, bottom, right);
    //指定为拉伸模式，伸缩后重新赋值
    return [image resizableImageWithCapInsets:insets resizingMode:UIImageResizingModeStretch];
}

@end
